package web;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.LoanService;
import util.ServiceFactory;

public class LoanServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String account=request.getParameter("account");
		double amount=Double.parseDouble(request.getParameter("amount"));
		LoanService service = ServiceFactory.getInstance(LoanService.class);
		try {
			double money=service.apply(account, amount);
			if(money==-1){
				request.setAttribute("result", "account not exist");
			}else if(money==-2){
				request.setAttribute("result", "贷款金额超过额度");
			}else{
				request.setAttribute("result", "申请通过,可以贷款的金额为:"+amount);
			}
			request.getRequestDispatcher("result.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e);
		}
	}
}
